from requests_html import HTMLSession
import pyperclip
import os

'''
m.58book.cc
'''

# 获取请求对象
session = HTMLSession()
headers = {
    "Referer": "hhttps://mip.58book.cc/book/20543.html",
    "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36"
}
url = pyperclip.paste()
# 往网站发送get请求
page = session.get(url, headers=headers)
page.encoding = 'utf-8'
content = page.html.find('ul.chapter', first=True)
title = page.html.find('div.block_txt2', first=True)
name = title.find('h2', first=True).text
# 下载地址
path = r'D:\.爬取为txt\{_name}\ '
path = path.format(_name=name)
if(os.path.exists(path) == False):
    os.makedirs(path)

a_s = content.find('a')
a_s = a_s[::-1]
for a in a_s:
    href = a.attrs['href']
    chap = a.text
    print('href ', href, a.text)
#     _page = session.get(href, headers=headers)
#     _page.encoding = 'utf-8'
#     text = _page.html.find('div#chaptercontent', first=True).text
#     # print(_page.html.find('div#chaptercontent', first=True).text)
#     href_2 = href.replace('.html', '_2.html')
#     text_2 = _page.html.find('div#chaptercontent', first=True).text
#     with open(path + f'{name}.txt', 'a', encoding='utf-8') as f:
#         f.write('\n')
#         f.write(chap)
#         f.write('\n\n')
#         f.write(text)
#         f.write(text_2)